package com.fs.web.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.common.pojo.Company;
import com.fs.web.base.enums.CodeEnum;
import com.fs.web.base.mapper.CompanyMapper;
import com.fs.web.base.utils.ApplicationUtils;
import com.fs.web.base.vo.JsonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.UUID;

/**
 * @author ACE
 * @desc 部门controller
 * @creationTime 2019-05-06 20:07
 * @projectName weixinframe
 * @packageName com.fs.web.base.controller
 */
@Slf4j
@Controller
@RequestMapping("/console/company/rewrite")
public class CompanyController {
    @Autowired
    private CompanyMapper companyMapper;

    @ResponseBody
    @RequestMapping("/newAdd")
    public JsonResponse newAdd(Company company){
        try {
            company.setId(UUID.randomUUID().toString().replaceAll("-",""));
            if(!company.getParentId().equals("1")){
                QueryWrapper<Company> wrapper = new QueryWrapper<>();
                wrapper.eq("id",company.getParentId());
                Company selectOne = companyMapper.selectOne(wrapper);
                company.setLine(selectOne.getLine()+company.getId()+"-");
                company.setLevel(selectOne.getLevel()+1);
            }else{
                company.setLevel(1);
                company.setLine(company.getId()+"-");
            }
            companyMapper.insert(company);
            return JsonResponse.success();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JsonResponse delete(){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            String id = map.get("id").toString();
            QueryWrapper<Company> wrapper = new QueryWrapper<>();
            wrapper.like("line", id);
            companyMapper.delete(wrapper);
            return JsonResponse.success();
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }

    @ResponseBody
    @RequestMapping("/prohibit")
    public JsonResponse prohibit(){
        try {
            Map<String, Object> map = ApplicationUtils.getRequestDatas();
            String id = map.get("id").toString();
            String companyStatus = map.get("companyStatus").toString();
            Company company = companyMapper.selectById(id);
            company.setCompanyStatus(companyStatus);
            companyMapper.updateById(company);
            return JsonResponse.success("操作成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            return JsonResponse.error(CodeEnum.EX999);
        }
    }
}
